<?php
/**
 * 后台系统公共控制器
 * @Author 聂哥<376927050@qq.com>
 * @Date: 2022-10-11 18:49:58
*/
namespace app\admin\controller\system;

use think\Controller;
use app\admin\model\system\SystemUser;
use app\admin\validate\system\User as UserValidate;
use think\facade\Session;

class Admin extends Controller
{
    /**
     * 系统登录
     * @MethodAuthor: 聂哥
     * @Date: 2022-10-15 17:15:55
     */
    public function login()
    {
        $user = new SystemUser();
        //是否已经登录
        if ($user->isLogin()) {
            dump($user->isLogin());
            $this->redirect('admin/system.index/index');
        }

        if ($this->request->isAjax()){
            //获取数据
            $data = $this->request->post();
            //验证
            $validate = new UserValidate();
            if(!$validate->scene('login')->check($data)) {
                return json(['code' => 0, 'msg' => $validate->getError(),'token'=> $this->request->token()]);
            }
           
            if (false == $user->login($data['username'],$data['password'])){
                return json(['code' => 0, 'msg' => $user->error,'token'=> $this->request->token()]);
            }
            $success = (string)url('/admin/system.index/index');
            return json(['code' => 1, 'msg' => '登录成功','url'=>$success]);
        }
        return $this->fetch();
    }

    /**
     * 登出
     */
    public function logout()
    {
        Session::delete('admin');
        return $this->redirect('/login');
    }

    /**
     * 管理员基本信息
     * @MethodAuthor: 聂哥
     * @Date: 2022-10-15 17:17:12
     */
    public function person()
    {

    }

    /**
     * 管理员基本信息
     * @MethodAuthor: 聂哥
     * @Date: 2022-11-02 17:17:12
     */
    public function noright()
    {
        return $this->fetch();
    }
}